﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace hello_accinv
{
    public partial class Discount : Form
    {
        private decimal price;

        public Discount()
        {
            InitializeComponent();
        }

        private void Discount_Load(object sender, EventArgs e)
        {
            price = Decimal.Parse(txtboxPrice.Text);
            txtboxDiscountBaht.MaxLength = txtboxPrice.Text.Length;
        }

        private bool IsNumeric(string input)
        {
            string validChars = "0123456789.";
            char inputChar;
            bool result = true;
            for (int i = 0; i < input.Length; i++)
            {
                inputChar = input[i];
                if (validChars.IndexOf(inputChar) == -1)
                {
                    result = false;
                    break;
                }
            }
            return result;
        }

        private void txtboxDiscountBaht_Leave(object sender, EventArgs e)
        {
            if (txtboxDiscountBaht.Text.Length > 0)
            {
                if (!IsNumeric(txtboxDiscountBaht.Text))
                {
                    txtboxDiscountBaht.Text = "";
                }
                else
                {
                    txtboxDiscountPercent.Text = ((Decimal.Parse(txtboxDiscountBaht.Text) / price) * Decimal.Parse("100")).ToString("0");
                }
            }
            else
            {
                txtboxDiscountPercent.Text = "";
            }
        }

        private void Discount_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                if (RegExFormatNumber(txtboxDiscountBaht.Text) && Decimal.Parse(txtboxDiscountBaht.Text) > 0)
                {
                    txtboxDiscountBaht.Text = Decimal.Parse(txtboxDiscountBaht.Text).ToString("#,#0.00#");
                }
                else if (RegExFormatNumber(txtboxDiscountPercent.Text))
                {
                    txtboxDiscountBaht.Text = ((Int32.Parse(txtboxDiscountPercent.Text) / 100) * price).ToString("#,#.00#");
                }

                if (!RegExFormatNumber(txtboxDiscountBaht.Text) ||
                    (txtboxDiscountBaht.Text.Length == 0) ||
                    (!IsNumeric(txtboxDiscountBaht.Text)) ||
                    (Decimal.Parse(txtboxDiscountBaht.Text) == 0) ||
                    (Decimal.Parse(txtboxDiscountBaht.Text) >= price))
                {
                    MessageBox.Show("Invalid");
                    e.Cancel = true;
                    txtboxDiscountPercent.Focus();
                }
            }
        }

        private void txtboxDiscountPercent_TextChanged(object sender, EventArgs e)
        {
            if (RegExFormatNumber(txtboxDiscountPercent.Text))
            {
                txtboxDiscountBaht.Text = ((Decimal.Parse(txtboxDiscountPercent.Text) / 100) * price).ToString("#,#.00#");
            }
            else
            {
                txtboxDiscountBaht.Text = "";
            }
        }

        public bool RegExFormatNumber(string text)
        {
            Regex rEx = new Regex("^[1-9][0-9]+(.[0-9]+)?$");
            return rEx.IsMatch(text);
        }
    }
}
